1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include<bits/stdc++.h> using namespace std; #define ID(x,y,z) ((z-1)*n*n + (x-1)*n + y)
const int ONE=70001; const int TWO=1000001; const int INF=1073741820;
int n,S,T; char ch[ONE],c; int a[45][45][45]; int next[TWO],first[TWO],go[TWO],w[TWO],tot; int q[2000001],tou,wei; int Dep[ONE],E[TWO]; int Ans;
int get() { int res=1,Q=1;char c; while( (c=getchar())<48 || c>57 ) if(c=='-')Q=-1; res=c-48; while( (c=getchar())>=48 && c<=57 ) res=res*10+c-48; return res*Q; }
void Add(int u,int v,int z) { next[++tot]=first[u]; first[u]=tot; go[tot]=v; w[tot]=z; next[++tot]=first[v]; first[v]=tot; go[tot]=u; w[tot]=0; }
void Double_Add(int u,int v,int z) { Add(u,v,z); Add(v,u,z); }
int PD(int x,int y,int z) { return (x+y+z)%2; }
int Bfs() { memset(Dep,0,sizeof(Dep)); tou=0; wei=1; Dep[0]=1; q[1]=0; for(int u=0;u<=T-1;u++) E[u]=first[u]; while(tou<wei) { int u=q[++tou]; for(int e=first[u];e;e=next[e]) { int v=go[e]; if(Dep[v] || !w[e]) continue; Dep[v]=Dep[u]+1; q[++wei]=v; } } return Dep[T]>0; }
int Dfs(int u,int Limit) { if(u==T || !Limit) return Limit; int flow=0,f; for(int &e=E[u];e;e=next[e]) { int v=go[e]; if(Dep[v]!=Dep[u]+1 || !w[e]) continue; f=Dfs(v,min(Limit,w[e])); w[e]-=f; w[((e-1)^1)+1]+=f; Limit-=f; flow+=f; if(!Limit) break; } return flow; }
int main() { cin>>n;
S=0; T=n*n*n+1; for(int z=1;z<=n;z++) for(int x=1;x<=n;x++) { scanf("%s",ch+1); for(int y=1;y<=n;y++) { if(ch[y]=='?') a[x][y][z]=1; if(ch[y]=='P') a[x][y][z]=2; if(ch[y]=='N') a[x][y][z]=3; } }
for(int z=1;z<=n;z++) for(int x=1;x<=n;x++) for(int y=1;y<=n;y++) { if(a[x+1][y][z]) Double_Add(ID(x,y,z),ID(x+1,y,z),1),Ans++; if(a[x][y+1][z]) Double_Add(ID(x,y,z),ID(x,y+1,z),1),Ans++; if(a[x][y][z+1]) Double_Add(ID(x,y,z),ID(x,y,z+1),1),Ans++;
if(a[x][y][z]==2) { if(PD(x,y,z)) Add(S,ID(x,y,z),INF); else Add(ID(x,y,z),T,INF); }
if(a[x][y][z]==3) { if(PD(x,y,z)) Add(ID(x,y,z),T,INF); else Add(S,ID(x,y,z),INF); } }
while(Bfs()) { Ans-=Dfs(S,INF); }
printf("%d",Ans); }
|